﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RPGX.ServerAdmin.Server;
using RPGXEngine.Server;
using RPGX.Data;
using RPGXEngine.Server.ActionMessages;

namespace RPGX.ServerAdmin
{
    public static class ServerRepository
    {
        #region Public Methods

        public static ServerStatus GetCurrentServerStatus()
        {
            RPGXAuthenticationEntities context = new RPGXAuthenticationEntities();

            var status = (from s in context.Servers
                          where s.Name.ToLower().Trim() == Configuration.GetCurrentServerName().Trim().ToLower()
                          select s.Status).FirstOrDefault();

            context.Dispose();

            return (ServerStatus)status;
        }

        public static int PostActionMessage(IServerActionMessage message)
        {
            RPGXServerEntities context = new RPGXServerEntities();

            ServerActionMessage m = new ServerActionMessage();
            m.Message = (int)message.Message;
            m.Data = message.Data;
            m.Stamp = DateTime.Now;
            m.Actioned = false;

            context.ServerActionMessages.AddObject(m);
            context.SaveChanges();

            context.Dispose();

            return m.ServerActionMessageId;
        }

        public static bool HasActionMessageBeenActioned(int actionMessageId)
        {
            RPGXServerEntities context = new RPGXServerEntities();

            // TODO throw an error here if the id is wrong
            // the problem here is if the id is wrong, then the default for bool may
            // be true, so the server process will be stopped if the id is wrong....
            bool actioned = (from m in context.ServerActionMessages
                             where m.ServerActionMessageId == actionMessageId
                             select m.Actioned).FirstOrDefault();

            context.Dispose();

            return actioned;
        }

        #endregion
    }
}
